package myself.a010BinaryTreeRecursion;

/**
 * @author 叶之越
 * Description
 * Date 2021/1/27
 * Time 20:22
 * Mail 739153436@qq.com
 */
public class IsBalanced {

    // 是否是平衡二叉树
    public static Info process2(Node random) {
        if (random == null) {
            return new Info(true, 0);
        }
        Info left = process2(random.left);
        Info right = process2(random.right);
        // 从底下往上推
        int curHeight = Math.max(left.height, right.height) + 1;
        boolean isBalance = true;
        if (!left.isBalaced
                || !right.isBalaced
                || Math.abs(left.height - right.height) > 1) {
            isBalance = false;
        }
        return new Info(isBalance, curHeight);
    }

    public static class Node {
        public int value;
        public Node left;
        public Node right;

        public Node(int data) {
            this.value = data;
        }
    }

    // 左、右要求一样，Info 信息返回的结构体
    public static class Info {
        public boolean isBalaced;
        public int height;

        public Info(boolean b, int h) {
            isBalaced = b;
            height = h;
        }
    }
}
